package com.meritdata.cloud.data.service.datadirectory.service.impl;

import com.meritdata.cloud.data.base.ResultData;
import com.meritdata.cloud.data.service.datadirectory.dao.IapplyDataDao;
import com.meritdata.cloud.data.service.datadirectory.dao.IcolumnDataDao;
import com.meritdata.cloud.data.service.datadirectory.entity.QTdDownloadPropose;
import com.meritdata.cloud.data.service.datadirectory.entity.TdDownloadColumnField;
import com.meritdata.cloud.data.service.datadirectory.entity.TdDownloadPropose;
import com.meritdata.cloud.data.service.datadirectory.service.IapplyDataService;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class applyDataService implements IapplyDataService {

    ResultData resultData = new ResultData();


    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private IapplyDataDao dao;

    @Autowired
    private IcolumnDataDao columndao;

    @Override
    public ResultData create(TdDownloadPropose downloadpropose,List<Long> cloumns) {
        downloadpropose.setApproveStatus(1);
        downloadpropose.setProposeTimes(0);
        dao.save(downloadpropose);
        resultData.setData(downloadpropose);
        long id = downloadpropose.getId();
        for(int i = 0;i<cloumns.size();i++){
            TdDownloadColumnField td = new TdDownloadColumnField();
            td.setProposeId(id);
            td.setColumnId(cloumns.get(i));
            td.setFormat("0");
            columndao.save(td);
            td=new TdDownloadColumnField();
        }
        return resultData;
    }

    /*
    * 获取数据库中最新数据
    * */
    @Override
    public List<TdDownloadPropose> getNewList(Map<String, Object> params) {
        QTdDownloadPropose downloadpropose=QTdDownloadPropose.tdDownloadPropose;
        Integer pageNum = Integer.parseInt((String) params.get("pages"));
        Integer sizeNum = Integer.parseInt((String) params.get("size"))-1;
        return jpaQueryFactory
                .selectFrom(downloadpropose)
                .offset(sizeNum)
                .limit(pageNum)
                .orderBy(
                        downloadpropose.createTime.asc()
                )
                .where()
                .fetch();
    }

    /*
     * 获取数据库中最热数据
     * */
    @Override
    public List<Tuple> getHotList(Map<String, Object> params) {
        QTdDownloadPropose downloadpropose=QTdDownloadPropose.tdDownloadPropose;
        Integer pageNum = Integer.parseInt((String) params.get("pages"));
        Integer sizeNum = Integer.parseInt((String) params.get("size"))-1;
        return jpaQueryFactory
                .select(downloadpropose.tableId.count(),downloadpropose.tableId)
                .from(downloadpropose)
                .offset(sizeNum)
                .limit(pageNum)
                .orderBy(
                        downloadpropose.tableId.count().asc()
                )
                .fetch();
//        List<TdDownloadPropose> tableIdlist = null;
//        tableIdlist.set(0,mylist.get(0));
//        int tableIdlistNum=10;
//        for(int i = 1;i<mylist.size();i++){
//            if(mylist.get(i).getTableId()==tableIdlist.get(i).tableId){
//                tableIdlist.set(i,mylist.get(i));
//            }
//            if(tableIdlist.size()==tableIdlistNum){
//                break;
//            }
//        }
//
//        return tableIdlist;

    }

    /*
     * 获取数据库中我的数据,根据表元数据id获取表信息
     * */
    @Override
    public List<TdDownloadPropose> getMyList(Map<String, Object> params) {
        QTdDownloadPropose downloadpropose=QTdDownloadPropose.tdDownloadPropose;
        Integer pageNum = Integer.parseInt((String) params.get("pages"));
        Integer sizeNum = Integer.parseInt((String) params.get("size"))-1;
        long myCreator =(long) params.get("creator");
        return jpaQueryFactory
                .selectFrom(downloadpropose)
                .where(downloadpropose.creator.eq(myCreator))
                .offset(sizeNum)
                .limit(pageNum)
                .fetch();
    }


}
